package cn.com.inossem.fragment;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.util.Base64;
import android.text.Html;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foxykeep.datadroid.requestmanager.Request;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.msg.MsgService;

import cn.com.inossem.activity.AddAccountActivity;
import cn.com.inossem.activity.CityActivity;
import cn.com.inossem.activity.JianJieActivity;
import cn.com.inossem.activity.ListJobsActivity;
import cn.com.inossem.activity.MainActivity;
import cn.com.inossem.activity.MessageActivity;
import cn.com.inossem.activity.MyInformationActivity;
import cn.com.inossem.activity.MyMomentsActivity;
import cn.com.inossem.activity.MyTEActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.activity.RegisteredActivity;
import cn.com.inossem.activity.RetrieveItemActivity;
import cn.com.inossem.activity.WebviewActivity;
import cn.com.inossem.activity.XiTongMessageActivity;
import cn.com.inossem.base.RequestFragment;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.DatePicjerInfo;
import cn.com.inossem.model.UserInfo;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.PackageUtil;
import cn.com.inossem.utils.Utils;

public class PersonalCenterFragment extends RequestFragment implements
		OnClickListener {

	public static PersonalCenterFragment center = null;

	LinearLayout shenqing, shoucang, myfriend, denglulayout, personallayout,
			shanchu;

	TextView dizhi, endriqi, registered;

	DatePicjerInfo mDatePicker;
	TextView nicheng, shenqingxiangmu, haoyounum, shoucangnum, zhuangtai;

	EditText zhanghao, mima;
	public TextView submit, wangji, num, xitongnum;

	ImageView incopic, more;

	RelativeLayout jianjie, xiaoxi, xitongxiaoxi, guwenshengya, mymoments;

	LinearLayout llayout01, llayout02, llayout03, llayout05, llayout09;

	RelativeLayout rlbut;

	RelativeLayout xiangmudi, endtimes;

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		if (isVisibleToUser) {
			mHasLoadedOnce = true;
		} else {
			mHasLoadedOnce = false;
		}
		super.setUserVisibleHint(isVisibleToUser);
	}

	public void updateNum() {
		String messagenumstr = Utils.messageInfo.userInfo.getMessageNum();
		int messageNum = Integer.parseInt(messagenumstr);
		xitongnum.setText(Utils.messageInfo.userInfo.getMessageNum());
		if (messageNum == 0) {
			xitongnum.setVisibility(View.GONE);
		} else {
			xitongnum.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		System.out.println("第4页" + mHasLoadedOnce);
		center = this;
		initview();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (Utils.messageInfo != null) {
			shenqingxiangmu.setText(Utils.messageInfo.userInfo.getApplyNum());
			haoyounum.setText(Utils.messageInfo.userInfo.getFriendNum());
			shoucangnum.setText(Utils.messageInfo.userInfo.getCollectNum());
			String messagenumstr = Utils.messageInfo.userInfo.getMessageNum();
			int messageNum = Integer.parseInt(messagenumstr);
			xitongnum.setText(Utils.messageInfo.userInfo.getMessageNum());
			if (messageNum == 0) {
				xitongnum.setVisibility(View.GONE);
			} else {
				xitongnum.setVisibility(View.VISIBLE);
			}
		}
		int unreadNum = NIMClient.getService(MsgService.class).getTotalUnreadCount();
		if (unreadNum == 0) {
			num.setVisibility(View.GONE);
		} else {
			num.setVisibility(View.VISIBLE);
		}
	}

	private void initview() {

		progressDialog = new ProgressDialog(getActivity());
		progressDialog.setMessage("正在加载中...");

		rlbut = (RelativeLayout) mRootView.findViewById(R.id.rlbut);
		rlbut.setOnClickListener(this);
		llayout01 = (LinearLayout) mRootView.findViewById(R.id.llayout01);
		llayout01.setOnClickListener(this);
		llayout02 = (LinearLayout) mRootView.findViewById(R.id.llayout02);
		llayout02.setOnClickListener(this);
		llayout03 = (LinearLayout) mRootView.findViewById(R.id.llayout03);
		llayout03.setOnClickListener(this);
		llayout05 = (LinearLayout) mRootView.findViewById(R.id.llayout05);
		llayout05.setOnClickListener(this);
		llayout09 = (LinearLayout) mRootView.findViewById(R.id.llayout09);
		llayout09.setOnClickListener(this);

		denglulayout = (LinearLayout) mRootView.findViewById(R.id.denglulayout);// 登录页面
		personallayout = (LinearLayout) mRootView
				.findViewById(R.id.personallayout);// 个人中心页面
		if (Utils.isDenglu == 1) {
			denglulayout.setVisibility(View.GONE);
			personallayout.setVisibility(View.VISIBLE);
		} else {
			denglulayout.setVisibility(View.VISIBLE);
			personallayout.setVisibility(View.GONE);
		}
		incopic = (ImageView) mRootView.findViewById(R.id.incopic);

		more = (ImageView) mRootView.findViewById(R.id.more);
		more.setOnClickListener(this);

		zhanghao = (EditText) mRootView.findViewById(R.id.zhanghao);
		mima = (EditText) mRootView.findViewById(R.id.mima);
		submit = (TextView) mRootView.findViewById(R.id.submit);
		submit.setOnClickListener(this);
		registered = (TextView) mRootView.findViewById(R.id.registered);
		registered.setOnClickListener(this);
		wangji = (TextView) mRootView.findViewById(R.id.wangji);
		wangji.setOnClickListener(this);

		mDatePicker = new DatePicjerInfo();
		nicheng = (TextView) mRootView.findViewById(R.id.nicheng);// 昵称
		shenqingxiangmu = (TextView) mRootView
				.findViewById(R.id.shenqingxiangmu);// 申请项目
		haoyounum = (TextView) mRootView.findViewById(R.id.haoyounum);// 好友个数
		shoucangnum = (TextView) mRootView.findViewById(R.id.shoucangnum);// 收藏个数

		xitongnum = (TextView) mRootView.findViewById(R.id.xitongnum);// 消息个数
		if (Utils.messageInfo != null) {
			String messagenumstr = Utils.messageInfo.userInfo.getMessageNum();
			int messageNum = Integer.parseInt(messagenumstr);
			xitongnum.setText(Utils.messageInfo.userInfo.getMessageNum());
			if (messageNum == 0) {
				xitongnum.setVisibility(View.GONE);
			} else {
				xitongnum.setVisibility(View.VISIBLE);
			}
		}

		num = (TextView) mRootView.findViewById(R.id.num);// 聊天个数
		int unreadNum = NIMClient.getService(MsgService.class)
				.getTotalUnreadCount();
		num.setText(unreadNum + "");
		if (unreadNum == 0) {
			num.setVisibility(View.GONE);
		} else {
			num.setVisibility(View.VISIBLE);
		}

		zhuangtai = (TextView) mRootView.findViewById(R.id.zhuangtai);// 状态
		dizhi = (TextView) mRootView.findViewById(R.id.dizhi);// 项目地
		endriqi = (TextView) mRootView.findViewById(R.id.endriqi);// 结束日期

		shanchu = (LinearLayout) mRootView.findViewById(R.id.shanchu);
		shanchu.setOnClickListener(this);
		shenqing = (LinearLayout) mRootView.findViewById(R.id.shenqing);
		shenqing.setOnClickListener(this);
		shoucang = (LinearLayout) mRootView.findViewById(R.id.shoucang);
		shoucang.setOnClickListener(this);
		myfriend = (LinearLayout) mRootView.findViewById(R.id.myfriend);
		myfriend.setOnClickListener(this);

		RelativeLayout muqianzhuangtai = (RelativeLayout) mRootView
				.findViewById(R.id.muqianzhuangtai);
		muqianzhuangtai.setOnClickListener(this);// 目前状态

		xiangmudi = (RelativeLayout) mRootView.findViewById(R.id.xiangmudi);
		xiangmudi.setOnClickListener(this);// 项目地

		endtimes = (RelativeLayout) mRootView.findViewById(R.id.endtimes);
		endtimes.setOnClickListener(this);// 项目结束日期

		RelativeLayout myinformation = (RelativeLayout) mRootView
				.findViewById(R.id.myinformation);
		myinformation.setOnClickListener(this);//
		mRootView.findViewById(R.id.te_mine).setOnClickListener(this);
		xiaoxi = (RelativeLayout) mRootView.findViewById(R.id.xiaoxi);
		xiaoxi.setOnClickListener(this);
		mymoments = (RelativeLayout) mRootView.findViewById(R.id.mymoments);
		mymoments.setOnClickListener(this);
		xitongxiaoxi = (RelativeLayout) mRootView
				.findViewById(R.id.xitongxiaoxi);
		xitongxiaoxi.setOnClickListener(this);

		guwenshengya = (RelativeLayout) mRootView
				.findViewById(R.id.guwenshengya);
		guwenshengya.setOnClickListener(this);

		jianjie = (RelativeLayout) mRootView.findViewById(R.id.jianjie);
		jianjie.setOnClickListener(this);

		if (Utils.messageInfo != null) {
			UserInfo ui = Utils.messageInfo.userInfo;
			if (!ui.getImageData().equals("")) {
				incopic.setBackgroundDrawable(new BitmapDrawable(Utils
						.toRoundCorner(stringtoBitmap(ui.getImageData()), 300)));
			}
			nicheng.setText(ui.getTruename());
			shenqingxiangmu.setText(ui.getApplyNum());
			haoyounum.setText(ui.getFriendNum());
			shoucangnum.setText(ui.getCollectNum());
			dizhi.setText(selectData_info("t_areas",
					"area_id=" + ui.getProjectAddress()));
			endriqi.setText(ui.getProjectEndDate());
			if (Utils.messageInfo.userInfo.getProjectStatus() == 0)
				zhuangtai.setText("");
			else
				zhuangtai.setText(selectData_info1("t_dictionary", "id="
						+ Utils.messageInfo.userInfo.getProjectStatus()));

			if (ui.getProjectStatus() == 10997) {
				dizhi.setText("");
				endtimes.setBackgroundColor(Color.parseColor("#E8E8E8"));
				endriqi.setText("");
				xiangmudi.setBackgroundColor(Color.parseColor("#E8E8E8"));
			} else {
				endtimes.setBackgroundColor(Color.parseColor("#FFFFFF"));
				xiangmudi.setBackgroundColor(Color.parseColor("#FFFFFF"));
			}

			String times = Utils.messageInfo.userInfo.getProjectEndDate();

			System.out.println(times.length() + "times:" + times);
			if (times.length() > 1) {
				String[] timesText = times.replace(".", "-").split("-");
				mDatePicker.year = Integer.parseInt(timesText[0]);
				mDatePicker.month = Integer.parseInt(timesText[1]) - 1;
				mDatePicker.dayofmonth = Integer.parseInt(timesText[2]);
			}
		}

	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	private String selectData_info1(String tabname, String selection) {
		String name = "";
		DatabaseHelper dh = new DatabaseHelper(getActivity());
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor.getColumnIndex("name_cn"));

		}
		cursor.close();
		db.close();
		dh.close();
		return name;
	}

	private String selectData_info(String tabname, String selection) {
		String name = "";
		DatabaseHelper dh = new DatabaseHelper(getActivity());
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor.getColumnIndex("area_name"));

		}
		cursor.close();
		db.close();
		dh.close();
		return name;
	}

	@Override
	public int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.fragment_personalcenter;
	}

	private void setInfo(String key, String value, String key1, String value1) {
		// 获得SharedPreferences对象
		SharedPreferences settings = getActivity().getSharedPreferences(
				"shared_file", 0);

		// 获得可编辑对象
		SharedPreferences.Editor editor = settings.edit();

		editor.putString(key, value);
		editor.putString(key1, value1);

		editor.commit();
	}

	private String getInfo(String key) {
		SharedPreferences settings = getActivity().getSharedPreferences(
				"shared_file", 0);
		// 第二个参数为如果SharedPreferences中没有保存就赋一个默认值
		String str = settings.getString(key, "");
		return str;
	}

	// 处理返回值
	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_LOGIN)) {

			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 1) {

				System.out.println("登录成功回调!");
				Utils.isDenglu = 1;
				setInfo("zhanghao", zhanghao.getText().toString(), "mima", mima
						.getText().toString());

				if (bundle.containsKey(WooRequestFactory.RESPONSE_LOGIN_DATA)) {
					Utils.messageInfo = bundle
							.getParcelable(WooRequestFactory.RESPONSE_LOGIN_DATA);
					setInfo();
				}

				if (bundle
						.containsKey(WooRequestFactory.PREFERENCES_SETING_DATA)) {
					JobsOpportunityFragment.rib = bundle
							.getParcelable(WooRequestFactory.PREFERENCES_SETING_DATA);
					// 插入数据库
					String[] str = JobsOpportunityFragment.rib.projectCycle
							.split(",");
					// 109周期108行业126模块127岗位//10924性质
					ContentValues contentValues = new ContentValues();
					contentValues.put("isselected", 1);

					ContentValues Values = new ContentValues();
					Values.put("isselected", 0);
					charu("t_dictionary", Values,
							"pid=126 or pid=127 or pid=109 or pid=108");

					charu("t_areas", Values, "");

					for (int i = 0; i < str.length; i++) {
						charu("t_dictionary", contentValues, "id=" + str[i]);
					}
					str = JobsOpportunityFragment.rib.projectIndustry
							.split(",");
					for (int i = 0; i < str.length; i++) {
						charu("t_dictionary", contentValues, "id=" + str[i]);
					}
					String _projectAddress = JobsOpportunityFragment.rib.projectAddress;
					if ((null != _projectAddress) && !_projectAddress.trim().equals("")) {
						str = _projectAddress.split(",");
						for (int i = 0; i < str.length; i++) {
							charu("t_dictionary", contentValues, "id=" + str[i]);
						}
					}
					str = JobsOpportunityFragment.rib.model.split(",");
					for (int i = 0; i < str.length; i++) {
						charu("t_dictionary", contentValues, "id=" + str[i]);
					}
					str = JobsOpportunityFragment.rib.station.split(",");
					for (int i = 0; i < str.length; i++) {
						charu("t_dictionary", contentValues, "id=" + str[i]);
					}

//					str = JobsOpportunityFragment.rib.projectAddress.split(",");
					charu("t_areas", Values, "");
					/*for (int i = 0; i < str.length; i++) {
						charu("t_areas", contentValues, "area_id=" + str[i]);
					}*/
					if ((null != _projectAddress) && !_projectAddress.trim().equals("")) {
						str = _projectAddress.split(",");
						for (int i = 0; i < str.length; i++) {
							charu("t_areas", contentValues, "area_id=" + str[i]);
						}
					}
				}
				zhanghao.clearFocus();
				mima.clearFocus();

				((MainActivity) getActivity()).doLogin();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 4) {
				Toast.makeText(getActivity(), "用户名已经存在！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 3) {
				Toast.makeText(getActivity(), "用户名格式错误！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 5) {
				Toast.makeText(getActivity(), "验证码错误！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 6) {
				Toast.makeText(getActivity(), "用户名密码错误！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 7) {
				Toast.makeText(getActivity(), "短信验证码获取错误！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			} else if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN) == 9) {
				Toast.makeText(getActivity(), "用户不存在！", Toast.LENGTH_LONG)
						.show();
				progressDialog.dismiss();
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_ZANSUCCESS)) {
			System.out.println("----------------------------------");
			Utils.isDenglu = 0;
			Utils.messageInfo = null;
			setInfo("zhanghao", "", "mima", "");
			denglulayout.setVisibility(View.VISIBLE);
			personallayout.setVisibility(View.GONE);
			NIMClient.getService(AuthService.class).logout();
		}

	}

	public void charu(String tabname, ContentValues contentValues,
	                  String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(getActivity());
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
			case R.id.xitongxiaoxi:
				intent = new Intent(getActivity(), XiTongMessageActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.xiaoxi:
				intent = new Intent(getActivity(), MessageActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.more:
				// intent = new Intent(getActivity(), CenterDialogActivity.class);
				// startActivityForResult(intent, 0);
				if (rlbut.getVisibility() == View.VISIBLE) {
					rlbut.setVisibility(View.GONE);
				} else {
					rlbut.setVisibility(View.VISIBLE);
				}
				break;
			case R.id.rlbut:
				rlbut.setVisibility(View.GONE);
				break;
			case R.id.llayout01:
				launchRequest(WooRequestFactory.zhuxiao());
				rlbut.setVisibility(View.GONE);
				break;
			case R.id.llayout02:
				intent = new Intent(getActivity(), AddAccountActivity.class);
				startActivityForResult(intent, 999);
				rlbut.setVisibility(View.GONE);
				break;
			case R.id.llayout03:
				intent = new Intent(getActivity(), AddAccountActivity.class);
				intent.putExtra("type", 1);
				startActivityForResult(intent, 999);
				rlbut.setVisibility(View.GONE);
				break;
			case R.id.llayout05:
				if (Utils.isDenglu == 0) {
					Toast.makeText(getActivity(), "请登录", Toast.LENGTH_LONG).show();
					return;
				} else {
					String[] email = {"zhiren@inossem.com"}; // 需要注意，email必须以数组形式传入
					intent = new Intent(Intent.ACTION_SEND);
					intent.setType("message/rfc822"); // 设置邮件格式
					intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
					intent.putExtra(Intent.EXTRA_SUBJECT, "关于智仁的意见"); // 主题
					intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<br/><br/><br/><br/><br/>来自用户：【" + Utils.messageInfo.userInfo.getUsername() + "】 【" + Utils.messageInfo.userInfo.getTruename() + "】"
							+ "<br/>邮件地址：【" + Utils.messageInfo.userInfo.getEmail() + "】")); // 正文
					startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
				}
				break;
			case R.id.llayout09:
				ContextThemeWrapper themedContext;
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
					themedContext = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
				} else {
					themedContext = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Light_NoTitleBar);
				}
				Builder builder = new AlertDialog.Builder(themedContext).setIcon(R.drawable.iconsmall);
//			AlertDialog.THEME_HOLO_LIGHT
				builder.setTitle("智仁");
				builder.setMessage("当前版本：v" + PackageUtil.getVersionName(getActivity()));
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				rlbut.setVisibility(View.GONE);
				builder.create().show();
				break;
			case R.id.shanchu:
				zhanghao.setText("");
				break;
			case R.id.submit:
				if (zhanghao.getText().toString().equals("")
						|| mima.getText().toString().equals("")) {
					Toast.makeText(getActivity(), "账号密码不能为空!", Toast.LENGTH_LONG)
							.show();
					return;
				} else {
					((MainActivity) getActivity()).guanbijianpan();
					progressDialog.show();
					launchRequest(WooRequestFactory.getLoginDataRequest(zhanghao
							.getText().toString(), mima.getText().toString()));
				}
				break;
			case R.id.registered:// 注册
				intent = new Intent(getActivity(), RegisteredActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.wangji:
				intent = new Intent(getActivity(), RegisteredActivity.class);
				intent.putExtra("type", 1);
				startActivity(intent);
				break;
			case R.id.myinformation:
				intent = new Intent(getActivity(), MyInformationActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.shenqing:
				intent = new Intent(getActivity(), ListJobsActivity.class);
				intent.putExtra("type", 1);
				startActivity(intent);
				break;
			case R.id.shoucang:
				intent = new Intent(getActivity(), ListJobsActivity.class);
				intent.putExtra("type", 0);
				startActivity(intent);
				break;
			case R.id.myfriend:
				((MainActivity) getActivity()).setPageView(1);
				break;
			case R.id.muqianzhuangtai:// 目前状态
				intent = new Intent(getActivity(), RetrieveItemActivity.class);
				intent.putExtra("type", 6);
				intent.putExtra("isflag", true);
				if (Utils.messageInfo.userInfo.getProjectStatus() == 0)
					intent.putExtra("idstr", "");
				else
					intent.putExtra("idstr",
							Utils.messageInfo.userInfo.getProjectStatus() + "");
				startActivityForResult(intent, 0);
				break;
			case R.id.xiangmudi:// 项目地
				System.out.println("Utils.messageInfo.userInfo.getProjectStatus():"
						+ Utils.messageInfo.userInfo.getProjectStatus());
				if (Utils.messageInfo.userInfo.getProjectStatus() == 10997) {
					Toast.makeText(getActivity(), "当前不在项目", Toast.LENGTH_LONG)
							.show();
					return;
				}
				intent = new Intent(getActivity(), CityActivity.class);
				intent.putExtra("type", 20);
				startActivityForResult(intent, 0);
				break;
			case R.id.endtimes:// 项目结束日期
				if (Utils.messageInfo.userInfo.getProjectStatus() == 10997) {
					Toast.makeText(getActivity(), "当前不在项目", Toast.LENGTH_LONG)
							.show();
					return;
				}
				DatePickerDialog datePickerDialog = new DatePickerDialog(
						getActivity(), AlertDialog.THEME_HOLO_LIGHT, Datelistener, mDatePicker.getYear(),
						mDatePicker.getMonth(), mDatePicker.getDayofmonth());
				datePickerDialog.show();
				break;
			case R.id.jianjie:
				intent = new Intent(getActivity(), JianJieActivity.class);
				startActivity(intent);
				break;
			case R.id.te_mine:
				intent = new Intent(getActivity(), MyTEActivity.class);
				startActivity(intent);
				break;
			case R.id.guwenshengya:
				intent = new Intent(getActivity(), WebviewActivity.class);
				startActivity(intent);
				break;
			case R.id.mymoments:
				intent = new Intent(getActivity(), MyMomentsActivity.class);
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
		/**
		 * params：view：该事件关联的组件 params：myyear：当前选择的年 params：monthOfYear：当前选择的月
		 * params：dayOfMonth：当前选择的日
		 */
		@Override
		public void onDateSet(DatePicker view, int myyear, int monthOfYear,
		                      int dayOfMonth) {

			// 修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
			mDatePicker.updateDate(myyear, monthOfYear, dayOfMonth);
			// 更新日期
			updateDate();

		}

		// 当DatePickerDialog关闭时，更新日期显示
		private void updateDate() {
			// 在TextView上显示日期
			String str = mDatePicker.getYear() + "-"
					+ (mDatePicker.getMonth() + 1) + "-"
					+ mDatePicker.getDayofmonth();
			endriqi.setText(str);

			launchRequest(WooRequestFactory.updatePersonal("projectEndDate",
					str));
		}
	};

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {

		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
			case 6:
				int id = intent.getIntExtra("id", 0);
				String name = intent.getStringExtra("name");
				zhuangtai.setText(name);
				if (Utils.messageInfo != null) {
					Utils.messageInfo.userInfo.setProjectStatus(id);

					if (Utils.messageInfo.userInfo.getProjectStatus() == 10997) {
						dizhi.setText("");
						endtimes.setBackgroundColor(Color.parseColor("#E8E8E8"));
						endriqi.setText("");
						xiangmudi.setBackgroundColor(Color.parseColor("#E8E8E8"));
					} else {
						endtimes.setBackgroundColor(Color.parseColor("#FFFFFF"));
						xiangmudi.setBackgroundColor(Color.parseColor("#FFFFFF"));
					}
				}
				break;
			case 20:
				id = intent.getIntExtra("id", 0);
				name = intent.getStringExtra("name");
				dizhi.setText(name);
				if (Utils.messageInfo != null)
					Utils.messageInfo.userInfo.setProjectAddress(id);
				break;
			case 80:
				launchRequest(WooRequestFactory.zhuxiao());
				break;
			case 81:
				break;
			case 82:
				break;
			case 99:
				setInfo();
				break;
			case 999:
				((MainActivity) getActivity()).doLogin();
				setInfo();
				break;
			default:
				break;
		}
	}

	public void setInfo() {
		if (Utils.isDenglu == 1) {
			denglulayout.setVisibility(View.GONE);
			personallayout.setVisibility(View.VISIBLE);
		} else {
			denglulayout.setVisibility(View.VISIBLE);
			personallayout.setVisibility(View.GONE);
		}
		if (Utils.messageInfo != null) {
			UserInfo ui = Utils.messageInfo.userInfo;
			if (!ui.getImageData().equals("")) {
				incopic.setBackgroundDrawable(new BitmapDrawable(Utils
						.toRoundCorner(stringtoBitmap(ui.getImageData()), 50)));
			}
			nicheng.setText(ui.getTruename());
			shenqingxiangmu.setText(ui.getApplyNum());
			haoyounum.setText(ui.getFriendNum());
			shoucangnum.setText(ui.getCollectNum());
			if (ui.getProjectStatus() == 0)
				zhuangtai.setText("");
			else
				zhuangtai.setText(selectData_info1("t_dictionary", "id=" + ui.getProjectStatus()));
			dizhi.setText(selectData_info("t_areas",
					"area_id=" + ui.getProjectAddress()));
			endriqi.setText(ui.getProjectEndDate());

			// String number = Utils.messageInfo.userInfo.getMessageNum();
			// int nums = Integer.parseInt(number) + Utils.liaotiannum;
			// num.setText(nums + "");

			xitongnum.setText(ui.getMessageNum());

			if (ui.getProjectStatus() == 10997) {
				dizhi.setText("");
				endtimes.setBackgroundColor(Color.parseColor("#E8E8E8"));
				endriqi.setText("");
				xiangmudi.setBackgroundColor(Color.parseColor("#E8E8E8"));
			} else {
				endtimes.setBackgroundColor(Color.parseColor("#FFFFFF"));
				xiangmudi.setBackgroundColor(Color.parseColor("#FFFFFF"));
			}
			String times = Utils.messageInfo.userInfo.getProjectEndDate();
			System.out.println("times:" + times);
			if (times.length() > 1) {
				String[] timesText = times.replace(".", "-").split("-");
				mDatePicker.year = Integer.parseInt(timesText[0]);
				mDatePicker.month = Integer.parseInt(timesText[1]) - 1;
				mDatePicker.dayofmonth = Integer.parseInt(timesText[2]);
			}

			((MainActivity) getActivity())
					.updatenNum(Utils.messageInfo.userInfo.getMessageNum(), false);
		}
	}
}
